function afterRender(grid)
{
	var gridCM = grid.getColumnModel();
	gridCM.setLocked(1, true);
}


//右击表头弹出菜单
//需要改进:表头名称不能有html标签元素，必须是纯文本;只能在IE使用，其它浏览器没兼容
function rightClickFn(grid,columnIndex,scope){
    scope.preventDefault();
	var gridCM = grid.getColumnModel();
	var gridDS = grid.getStore(); 
	var e = event.srcElement?event.srcElement:event.target;
	if(document.getElementById("edit") != null)
	{
		this.focus();	
		return false;
	}
	var rightClick = new Ext.menu.Menu({ 
		id:'rightClickMenu',  
		items: [ 
	      { 
	    	  id: 'ascSort', 
	    	  handler:function(){sort(grid,columnIndex,e,"asc");}, 
	    	  text: '升序排列',
	    	  icon: getRootPath()+"/images/icon/asc.gif"
	      },
	      { 
	    	  id: 'descSort', 
	    	  handler:function(){sort(grid,columnIndex,e,"desc");}, 
	    	  text: '降序排列',
	    	  icon: getRootPath()+"/images/icon/desc.gif"
	      },
	      /*{ 
	    	  id: 'delSort', 
	    	  handler:function(){deleteSort(grid,e);}, 
	    	  text: '取消排序',
	    	  icon: getRootPath()+"/images/icon/asc.gif"
	      },*/
	      {
	    	  itemId: 'lock', 
	    	  handler:function(){lock(grid,columnIndex);}, 
	    	  text: '锁列', 
	    	  cls: 'xg-hmenu-lock'
		  },
		  {
	    	  itemId: 'unlock', 
	    	  handler:function(){unlock(grid,columnIndex);}, 
	    	  text: '解锁列', 
	    	  cls: 'xg-hmenu-unlock'
		  },
		  { 
			  id: 'addRow', 
			  handler:function(){addRow(grid,e,gridCM,gridDS);}, 
			  text: '插入行（上方）',
			  icon: getRootPath()+"/images/icon/charu_1.png"
		  }, 
		  { 
			  id: 'deleteRow', 
			  handler: function(){deleteRow(grid,e,gridCM,gridDS);}, 
			  text: '删除行',
			  icon: getRootPath()+"/images/icon/delete_1.gif"
		  } ,
		  { 
			  id: 'rightMergeCell', 
			  handler: function(){rightMergeCell(grid,e,gridCM,gridDS);}, 
			  text: '右合并单元格',
			  icon: getRootPath()+"/images/icon/hebing_1.gif" 
		  } ,
		  { 
			  id: 'horizontalSplitCell', 
			  handler: function(){horizontalSplitCell(grid,e,gridCM,gridDS);}, 
			  text: '拆分单元格',
			  icon: getRootPath()+"/images/icon/chaifen_1.gif" 
		  } ,
		  { 
			  id: 'editColumnName', 
			  handler: function(){editColumnName(grid,e,gridCM,gridDS);}, 
			  text: '编辑列名',
			  icon: getRootPath()+"/images/icon/bianji_1.png"
		  } 
		] 
		}); 
	
	var rowIndex = getGridRowIndex(e);
	if(rowIndex == 0 && grid.url=="/detailReport.report") //明细报表右键菜单
	{
		rightClick.items.get('deleteRow').hidden = true;
		rightClick.items.get('rightMergeCell').hidden = true;
		rightClick.items.get('horizontalSplitCell').hidden = true;
		rightClick.items.get('editColumnName').hidden = true;
		rightClick.items.get('addRow').hidden = true;
	}else if(rowIndex == 0){ //汇总第一行
		rightClick.items.get('deleteRow').hidden = true;
		rightClick.items.get('rightMergeCell').hidden = true;
		rightClick.items.get('ascSort').hidden = true;
		rightClick.items.get('descSort').hidden = true;
		rightClick.items.get('lock').hidden = true;
		rightClick.items.get('unlock').hidden = true;
		rightClick.items.get('horizontalSplitCell').hidden = true;
		rightClick.items.get('editColumnName').hidden = true;
	}else{ //汇总复杂表头
		rightClick.items.get('ascSort').hidden = true;
		rightClick.items.get('descSort').hidden = true;
		rightClick.items.get('lock').hidden = true;
		rightClick.items.get('unlock').hidden = true;
		var rowsSize = gridCM.rows.length ;
		var index = rowsSize - rowIndex;
		var clickCellIndex = getClickCellIndex(e);
		var clickColumnSize = gridCM.rows[index].length;		//计算所点击行的列数
		if(clickCellIndex == clickColumnSize - 1)
		{
			rightClick.items.get('rightMergeCell').disabled = true;
		}
		if(gridCM.rows[index][clickCellIndex].colspan == 1)
		{
			rightClick.items.get('horizontalSplitCell').disabled = true;
		}
	}
	rightClick.showAt(scope.getXY());
	rightClick.on("beforehide",function(e){
		if(rightClick != null)
		{
			rightClick.removeAll();
		}
	});
}
//双击表头修改列名
function headerDbclickFn(grid,columnIndex,scope)
{
	var e = event.srcElement?event.srcElement:event.target;
	if(document.getElementById("edit") != null)
	{
		return false;
	}
	var gridCM = grid.getColumnModel();
	var gridDS = grid.getStore();
	editColumnName(grid,e,gridCM,gridDS);
}
//单击表头时获取焦点
function headerClickFn(grid,columnIndex,scope)
{
	var e = event.srcElement?event.srcElement:event.target;
	e.focus();
}

//排序
function sort(grid,columnIndex,e,order)
{
	var iColIndex = grid.getColumnModel().getColumnCount(); //grid的列数
	var orderFlag = false;
	orderbys='';
	for(var i = 0; i< iColIndex; i++){
		var colname = grid.getColumnModel().getDataIndex(i); //获取列名   
		if(columnIndex==i)
		{
			orderFlag = true;
			orderbys += colname +" "+order;  //默认升序
		}
	}
	//alert(orderbys);
	if(!orderFlag)//如果没有排序
		orderbys = "";
	//刷新表格
	grid.baseParams.method = "sort";
	grid.baseParams.dataSource = dataSource;
	grid.baseParams.orderbys=orderbys;
	grid.load();
}

//清除排序
function deleteSort(grid,e)
{
	orderbys='';
	//刷新表格
	grid.baseParams.method = "sort";
	grid.baseParams.dataSource = dataSource;
	grid.baseParams.orderbys=orderbys;
	grid.load();
}

//
function lock(grid,columnIndex)
{
	var gridCM = grid.getColumnModel();
	gridCM.setLocked(columnIndex, true);
	var locks = [];
	for(var i=0;i<gridCM.config.length;i++){
		if(gridCM.config[i].locked)
			locks.push(gridCM.config[i].dataIndex);
	}
	lockedField = locks.join(','); 
}

function unlock(grid,columnIndex)
{
	var gridCM = grid.getColumnModel();
	gridCM.setLocked(columnIndex, false);
	var locks = [];
	for(var i=0;i<gridCM.config.length;i++){
		if(gridCM.config[i].locked)
			locks.push(gridCM.config[i].dataIndex);
	}
	lockedField = locks.join(','); 
}

//插入行
function addRow(grid,e,gridCM,gridDS)
{
	//var clone = tableNode.cloneNode(true);
	//tableNode.parentNode.insertBefore(clone,tableNode);
	var rowIndex = getGridRowIndex(e);
	var rowsSize = gridCM.rows.length ;
	var index = 0;//插入行的位置
	if(rowIndex == 0)
	{
		index = rowsSize;
	}
	else if(rowIndex >0)
		{			
			index = rowsSize - rowIndex;
		}
	//插入空白行
	var spaceRow = [];
	var columnSize = gridCM.columns.length;
	for(var i = 0;i < columnSize;i++)
	{
		spaceRow[i] = {header:'',align:'center'};
	}
	gridCM.rows.splice(index,0,spaceRow);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}

function updateUnit(grid,e,gridCM,gridDS,columnIndex)
{
	var step = "";
	var unitForm = new Ext.FormPanel({
		id:'unitForm',
		frame:true,
		items:[{
			id:'stepNum',
			xtype:'textfield',
			fieldLabel:'缩小倍数',
			labelStyle:'text-align:right;',
			width:150
		}],
	    buttons:[{
		    text:'确定', 
		    xtype:'button',
		    handler:function(){
	    		step = Ext.getCmp("stepNum").getValue();
	    		var rowIndex = getGridRowIndex(e);
	    		var clickCellIndex = getClickCellIndex(e);
	    		if(rowIndex < 0)
	    		{
	    			return false;
	    		}else
	    		{
	    			var reg = /^[1-9][0-9]*$/;
	    			var match = step.match(reg);
	    			if(match==null)
	    			{
	    				alert("请输入数字！");
	    				return false;
	    			}else
	    			{
	    				var val = document.getElementById("updateUnit").value;
	    				if(step=="")
	    				{
	    					alert("缩小倍数不能为空！");
	    				}else
	    				{
	    					 if(val=="")
	    						 document.getElementById("updateUnit").value = columnIndex + "@" + step;
	    					 else
	    					 {
	    						 var split = val.split(",");
	    						 var bool = false;
	    						 for(var i=0;i<split.length;i++)
	    						 {
	    							 if(split[i].split("@")[0]==columnIndex)
	 	    							bool = true;
	    						 }
	 	    					if(!bool)
	 	    					{
	 	    						val = val + "," + columnIndex+ "@" + step;
	 	    						//document.getElementById("updateUnit").value = val;
	 	    					}
	    					 }
	    						
	    				}
	    			}
	    			
	    		}
	    		unitWin.close();
		     }
		},{
		    text:'取消', 
		    xtype:'button',
		    handler:function(){
				unitWin.close();
			}
		}]
	});	

	var unitWin = new Ext.Window({//添加/修改菜单弹出窗口
		id:'unitFormWin',
		title:'缩小倍数设置',
		width: 400,
		height:120,
		layout: 'fit',
		modal:true,
		closeAction:'close',
		buttonAlign:'center',
		resizable:false,
		items: [unitForm]
	});	
	unitWin.show();
	
}

//删除行
function deleteRow(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	var rowsSize = gridCM.rows.length ;
	if(rowIndex <= 0)
	{
		return false;
	}
	var index = rowsSize - rowIndex;  //计算要删除的行序号
	gridCM.rows.splice(index,1);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}
//向右合并单元格
function rightMergeCell(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	if(rowIndex <= 0)
	{
		return false;
	}
	var rowsSize = gridCM.rows.length ;
	var index =  rowsSize - rowIndex;  //计算要合并的行序号
	var clickCellIndex = getClickCellIndex(e);
	var clickColumnSize = gridCM.rows[index].length;		//计算所点击行的列数
	if(clickCellIndex == clickColumnSize - 1)
	{
		return false;
	}
	var nextColspan = gridCM.rows[index][clickCellIndex+1].colspan ;	//得到点击列的右边列的colspan
	var nextHeader = gridCM.rows[index][clickCellIndex+1].header ;	//得到点击列的右边列的header
	gridCM.rows[index][clickCellIndex].colspan = gridCM.rows[index][clickCellIndex].colspan + nextColspan;
	gridCM.rows[index][clickCellIndex].header = gridCM.rows[index][clickCellIndex].header + nextHeader;
	gridCM.rows[index].splice(clickCellIndex+1,1);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}
//编辑列名
function editColumnName(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	if(grid.enableHeadEdit && rowIndex == 0)
	{
		return false;
	}
	var newInput = "<input id='edit' style='width:100%' onblur = '' value="+e.innerText+">";
    var dh = Ext.DomHelper;
	e.innerText = "";
	dh.append(e,newInput);
	var editElement = document.getElementById("edit");
	editElement.focus();
	editElement.focus();
	editElement.onblur=function(){inputToLable(grid,e,rowIndex,gridCM,gridDS);};
}
function inputToLable(grid,e,rowIndex,gridCM,gridDS)
{
	var text = document.getElementById("edit").value;
	var rowsSize = gridCM.rows.length ;
	var index =  rowsSize - rowIndex;  //计算要合并的行序号
	var clickCellIndex = getClickCellIndex(e);
	if(rowIndex == 0){
		//第一行普通表头
		gridCM.setColumnHeader(clickCellIndex,text);
	}else{
		//复杂表头
		gridCM.rows[index][clickCellIndex].header = text ;
		grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
	}
}
//横向拆分单元格
function horizontalSplitCell(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	if(rowIndex <= 0)
	{
		return false;
	}
	var rowsSize = gridCM.rows.length ;
	var index =  rowsSize - rowIndex;  //计算要合并的行序号
	var clickCellIndex = getClickCellIndex(e);
	var colspan = gridCM.rows[index][clickCellIndex].colspan;
	if(colspan == 1)
	{
		return false;
	}
	gridCM.rows[index][clickCellIndex].colspan = 1;
	var splitCell = {header:'',align:'center',colspan:''};
	splitCell.colspan = colspan - 1;
	gridCM.rows[index].splice(clickCellIndex+1,0,splitCell);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}
//获取所点击的行序号
function getGridRowIndex(e)
{
	var tableNode = e.parentNode.parentNode.parentNode.parentNode;
	var divNode = tableNode.parentNode;
	var rowIndex = 0;	
	for(var i = 0 ,len = divNode.childNodes.length;i < len;i++)
	{
		if(divNode.childNodes[i] == tableNode)
		{
			rowIndex = len - i -1;
			break;
		}
	}
	return rowIndex;
}
//计算点击列在当前行数组(cm.rows)中的序号
function getClickCellIndex(e)
{
	var clickCellIndex = 0; 
	var theadNode = e.parentNode.parentNode;
	for(var n = 0 ,len = theadNode.childNodes.length;n < len; n++)
	{
		if(e.parentNode == theadNode.childNodes[n])
		{
			clickCellIndex = n;
			break;
		}
	}
	return clickCellIndex;
}